<template>
    <div class="shopcar" @click="toggleList">
        <transition name="fade">
            <div class="list-mark" @click="!listShow" v-show="listShow"></div>
        </transition>
        <div class="content">
            <div class="content-left">
                <div class="logo-wrapper">
                    <div class="logo">
                        <span class="icon-shopping_cart"></span>
                    </div>
                    <div class="num">{{totalCount}}</div>
                </div>
                <div class="price" :class="payClass">￥{{totalPrice}}</div>
                <div class="desc">另需配送费￥{{Dseller.deliveryPrice}}元</div>
            </div>
            <div class="content-right">
                <div class="pay" :class="payClass" @click.stop="pay">{{payDesc}}</div>
            </div>
        </div>
        <transition name="my">
                    <div class="shopcart-list" v-show="listShow" @click.stop>
            <div class="list-header">
                <h1 class="title">购物车</h1>
                <span class="empty" @click="empty">清空</span>
            </div>
            <div class="list-content">
                <ul>
                    <li class="food" v-for="food in selectedFoods" :key="food.name">
                        <span class="name">{{food.name}}</span>
                        <div class="price">
                            <span>￥{{food.price*food.count}}</span>
                        </div>
                        <div class="cartcontrol-wrapper">
                            <cartcontrol :food="food"></cartcontrol>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
        </transition>
    </div>
</template>

<script>
import cartcontrol from '../cartcontrol/cartcontrol.vue'
import BScroll from 'better-scroll'
export default {
  props: ['Dseller', 'selectedFoods'],
  computed: {
    totalPrice() {
      let total = 0
      this.selectedFoods.forEach(food => {
        total += food.price * food.count
      })
      return total
    },
    totalCount() {
      let count = 0
      this.selectedFoods.forEach(food => {
        count += food.count
      })
      return count
    },
    payDesc() {
      if (this.totalPrice === 0) {
        return `￥${this.Dseller.minPrice}元起送`
      } else if (this.totalPrice < this.Dseller.minPrice) {
        let diff = this.Dseller.minPrice - this.totalPrice
        return `还差${diff}元起送`
      } else {
        return `去结算`
      }
    },
    payClass() {
      if (this.totalPrice >= this.Dseller.minPrice) {
        return 'outer'
      }
    },
    listShow() {
      if (!this.totalPrice) {
        return false
      }
      return !this.fold
    }
  },
  components: {
    cartcontrol
  },
  data() {
    return {
      fold: true
    }
  },
  methods: {
    toggleList() {
      if (!this.totalPrice) {
        return
      }
      this.fold = !this.fold
    },
    empty() {
      this.selectedFoods.forEach(food => {
        food.count = 0
      })
    },
    pay() {
      window.alert(`支付${this.totalPrice}元`)
    },
    initScroll() {
      this.$nextTick(() => {
        if (!this.scroll) {
          this.scroll = new BScroll(document.querySelector('.list-content'), {
            click: true
          })
          this.scroll.refresh()
          console.dir(document.querySelector('.list-content'))
        } else {
          console.log(3)
          this.scroll.refresh()
        }
      })
    }
  },
  // mounted() {
  //   this.$nextTick(() => {
  //     this.initScroll()
  //   })
  // }
  watch: {
    Dseller() {
      console.log(1)
      this.$nextTick(() => {
        console.log(2)
        this.initScroll()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../common/scss/basees.scss";
.my-enter,
.my-leave-to {
  transform: translateY(100%);
  opacity: 0;
}
.my-enter-active,
.my-leave-active {
  transition: all 0.4s linear;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
.fade-enter-active,
.fade-leave-active {
  transition: all 0.4s linear;
}

.shopcar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 48px;
  background-color: #000;
  .list-mark {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 40;
    background-color: rgba(7, 17, 27, 0.6);
  }
  .content {
    position: relative;
    z-index: 50;
    display: flex;
    background-color: #141d27;
    font-size: 0;
    .content-left {
      flex: 1;
      .logo-wrapper {
        display: inline-block;
        position: relative;
        top: -10px;
        margin: 0 12px;
        padding: 6px;
        width: 56px;
        height: 56px;
        box-sizing: border-box;
        vertical-align: top;
        border-radius: 50%;
        background-color: #141d27;
        .logo {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background-color: #2b343c;
          &.highlight {
            background-color: rgb(0, 160, 220);
            color: #fff;
          }
        }
        .num {
          font-size: 9px;
          font-weight: 700;
          color: #fff;
          line-height: 16px;
          position: absolute;
          top: 0;
          right: 0;
          background-color: rgb(240, 20, 20);
          box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.4);
          width: 24px;
          height: 16px;
          border-radius: 16px;
          text-align: center;
        }
      }
      .price {
        display: inline-block;
        vertical-align: top;
        line-height: 24px;
        font-weight: 700;
        margin-top: 12px;
        box-sizing: border-box;
        padding-right: 12px;
        border-right: 1px solid rgba(255, 255, 255, 0.1);
        font-size: 16px;
        color: rgba(255, 255, 255, 0.4);
        &.outer {
          color: #fff;
        }
      }
      .desc {
        display: inline-block;
        vertical-align: top;
        line-height: 24px;
        margin: 12px 0 0 12px;
        color: rgba(255, 255, 255, 0.4);
        font-size: 16px;
        font-weight: 700;
      }
    }
    .content-right {
      flex: 0 0 105px;
      width: 105px;
      .pay {
        height: 48px;
        line-height: 48px;
        text-align: center;
        font-size: 12px;
        color: rgba(255, 255, 255, 0.4);
        font-weight: 700;
        background-color: #2b333c;
        &.outer {
          background-color: green;
          color: #fff;
          font-size: 16px;
        }
      }
    }
  }
  .shopcart-list {
    position: absolute;
    left: 0;
    bottom: 48px;
    width: 100%;
    z-index: 41;
    background-color: #fff;
    .list-header {
      background-color: #f3f5f7;
      width: 100%;
      height: 40px;
      display: flex;
      align-items: center;
      position: relative;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      .title {
        font-size: 14px;
        font-weight: 200;
        color: rgb(7, 17, 27);
        position: absolute;
        left: 18px;
      }
      .empty {
        font-size: 12px;
        font-weight: 700;
        position: absolute;
        right: 18px;
        color: rgb(0, 160, 220);
      }
    }
    .list-content {
      max-height: 243px;
      overflow: hidden;
      .food {
        height: 48px;
        width: 100%;
        display: flex;
        align-items: center;
        background-color: #fff;
        border-bottom: 1px solid rgba(7, 17, 27, 0.1);
        box-sizing: border-box;
        .name {
          font-size: 14px;
          color: rgb(7, 17, 27);
          position: absolute;
          left: 18px;
          font-weight: 700;
        }
        .price {
          font-size: 14px;
          font-weight: 700;
          color: rgb(240, 20, 20);
          position: absolute;
          right: 114px;
        }
        .cartcontrol-wrapper {
          position: absolute;
          right: 18px;
        }
      }
    }
  }
}
</style>